1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * PseudoRandomDataSource.java
6    *
7    * Created on 4-Jan-2005 at 2:59:43 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import org.springframework.beans.factory.InitializingBean;
12  
13  import ca.uhn.cache.IDataSource;
14  import ca.uhn.cache.IQuery;
15  import ca.uhn.cache.IQueryResult;
16  import ca.uhn.cache.exception.DataSourceException;
17  
18  
19  /***
20   * Sleeps for a period of time before returning a predefined <code>QueryResult</code>. 
21   * 
22   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
23   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:48 $ by $Author: bryan_tripp $
24   */
25  public class DelayedDataSource implements IDataSource, InitializingBean {
26  
27      private IQueryResult myQueryResult;
28      private int myResponseDelay = -1;
29      
30      /***
31       */
32      public DelayedDataSource() {
33      }
34      
35      /***
36       * {@inheritDoc}
37       */
38      public IQueryResult execute( IQuery[] theQueries ) throws DataSourceException {
39          try {
40              Thread.sleep( getResponseDelay() * 1000 );
41          }
42          catch (InterruptedException e) {
43          }
44          
45          return getQueryResult();
46      }
47  
48      /***
49       * @return Returns the queryResult.
50       */
51      public IQueryResult getQueryResult() {
52          return myQueryResult;
53      }
54      /***
55       * @param theQueryResult The queryResult to set.
56       */
57      public void setQueryResult( IQueryResult theQueryResult ) {
58          myQueryResult = theQueryResult;
59      }
60      /***
61       * @return Returns the responseDelay.
62       */
63      public int getResponseDelay() {
64          return myResponseDelay;
65      }
66      /***
67       * @param theResponseDelay The responseDelay to set.
68       */
69      public void setResponseDelay( int theResponseDelay ) {
70          myResponseDelay = theResponseDelay;
71      }
72  
73      /***
74       * {@inheritDoc}
75       */
76      public void afterPropertiesSet() throws Exception {
77          //assert getQueryResult() != null;
78          assert getResponseDelay() != -1;
79      }
80  }